{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f22941c0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import scipy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1f42a740",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "#tumor stage and treatment_type\n",
    "Liu_clinical=pd.read_csv('Liu_data/Liu_clinical.csv')\n",
    "#because we don't know the tumor stage in dataset liu, we assign the tumor size 1.4e7, which is the median size of tumor in tcga\n",
    "\n",
    "Liu_clinical['tumor_size']=1.400000e+07\n",
    "Liu_clinical_sub=Liu_clinical[['Patient','tumor_size','OS','BR','cyclesOnTherapy']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "41f9c8aa",
   "metadata": {},
   "outputs": [],
   "source": [
    "#ssGSEA of MHC-I and MHC-II\n",
    "Liu_mhc=pd.read_csv('Liu_data/Liu_mhc.csv')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "cfb0d81a",
   "metadata": {},
   "outputs": [],
   "source": [
    "#abstract the pd-1 expression \n",
    "#Liu_genes=pd.read_csv('data/Liu_genes.csv')\n",
    "# PDCD1=Liu_genes[Liu_genes['Unnamed: 0']=='PDCD1'].T\n",
    "# PDCD1=PDCD1.reset_index()\n",
    "# PDCD1.to_csv('data/Liu_PDCD1.csv')\n",
    "Liu_pd=pd.read_csv('Liu_data/Liu_PDCD1.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9be42318",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Patient</th>\n",
       "      <th>SC T.CD8</th>\n",
       "      <th>T cells regulatory (Tregs)</th>\n",
       "      <th>T cells CD8</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Patient145</td>\n",
       "      <td>0.542359</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.313061</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Patient125</td>\n",
       "      <td>2.685226</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.367949</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Patient100</td>\n",
       "      <td>1.893131</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.114949</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      Patient  SC T.CD8  T cells regulatory (Tregs)  T cells CD8\n",
       "0  Patient145  0.542359                         0.0     0.313061\n",
       "1  Patient125  2.685226                         0.0     2.367949\n",
       "2  Patient100  1.893131                         0.0     2.114949"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#from the cibersort analysis import the information of treg\n",
    "#from the time signature import the informaiton of cd8+T \n",
    "Liu_time=pd.read_csv('Liu_data/Liu_TIME.csv')\n",
    "Liu_time_sub=Liu_time[['Patient','SC T.CD8','T cells regulatory (Tregs)','T cells CD8']]\n",
    "Liu_time_sub.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "59c56fce",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "#merge all the table above\n",
    "Liu_all=pd.merge(Liu_clinical_sub,Liu_mhc,on='Patient',how='inner')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "fd119db7",
   "metadata": {},
   "outputs": [],
   "source": [
    "Liu_all=pd.merge(Liu_all,Liu_pd,on='Patient',how='inner')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "15c811be",
   "metadata": {},
   "outputs": [],
   "source": [
    "Liu_all=pd.merge(Liu_all,Liu_time_sub,on='Patient',how='inner')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "4dadce18",
   "metadata": {},
   "outputs": [],
   "source": [
    "#calculate the ratio of Treg to CD8+T in cibersort result\n",
    "Liu_all['Treg_pro']=Liu_all['T cells regulatory (Tregs)']/Liu_all['T cells CD8']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e7da8c44",
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "#deal with abnormal value of Treg_pro\n",
    "for i in range(len(Liu_all)):\n",
    "    Treg_pro=Liu_all.loc[i,'Treg_pro']\n",
    "    if Treg_pro>=0 and Treg_pro<100:\n",
    "        pass\n",
    "    else:\n",
    "        Liu_all.loc[i,'Treg_pro']=0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "67b2d3d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "#calculate total MHC expression in ssGSEA result\n",
    "Liu_all['mhc']=Liu_all['MHC-I']+Liu_all['MHC-II']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "2644886b",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "x=Liu_all['mhc'].tolist()\n",
    "y=Liu_all['SC T.CD8']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "262b667e",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PearsonRResult(statistic=0.6742659420513787, pvalue=3.1374521291902884e-17)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#correlation of MHC expression and CD8+T cells' scores\n",
    "r = np.corrcoef(x, y)\n",
    "scipy.stats.pearsonr(x, y) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "68560fef",
   "metadata": {},
   "outputs": [],
   "source": [
    "#Because the expression of PDCD1 is long tail,normalize it by np.log(x+1)/np.log(max(x)+1)\n",
    "Liu_all['PDCD1'].describe()\n",
    "Liu_all['mu']=np.log(Liu_all['PDCD1']+1)/8.83"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "c1c1cf8c",
   "metadata": {},
   "outputs": [],
   "source": [
    "#normalize the MHC expression\n",
    "Liu_all['mhc_norm']=(Liu_all['mhc']+22245)/(22210+22245)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "8d32ef1e",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    120.000000\n",
       "mean       0.034480\n",
       "std        0.592138\n",
       "min       -1.059181\n",
       "25%       -0.401139\n",
       "50%       -0.072303\n",
       "75%        0.341287\n",
       "max        2.685226\n",
       "Name: SC T.CD8, dtype: float64"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Liu_all['SC T.CD8'].describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "f06ae160",
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "cd8t_std=0.03/(2.231578+3.384689)\n",
    "cd8t_mean=cd8t_std*2.231578\n",
    "Liu_all['tcd8_pop']=(Liu_all['SC T.CD8']*cd8t_std+cd8t_mean)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "c2717953",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    120.000000\n",
       "mean       0.012104\n",
       "std        0.003163\n",
       "min        0.006263\n",
       "25%        0.009778\n",
       "50%        0.011534\n",
       "75%        0.013743\n",
       "max        0.026264\n",
       "Name: tcd8_pop, dtype: float64"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Liu_all['tcd8_pop'].describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "4bc4aff0",
   "metadata": {},
   "outputs": [],
   "source": [
    "Liu_all.to_csv('Liu_all.csv')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
